1 /*
2  * Copyright (C) 2012 Red Hat Inc.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  *     * Redistributions of source code must retain the above
9  *       copyright notice, this list of conditions and the
10  *       following disclaimer.
11  *     * Redistributions in binary form must reproduce the
12  *       above copyright notice, this list of conditions and
13  *       the following disclaimer in the documentation and/or
14  *       other materials provided with the distribution.
15  *     * The names of contributors to this software may not be
16  *       used to endorse or promote products derived from this
17  *       software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
26  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
29  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
30  * DAMAGE.
31  *
32  * Author: Stef Walter <stefw@redhat.com>
33  */
34 
35 #include "config.h"
36 
37 #include "hash.h"
38 #include "oid.h"
39 
40 #include <assert.h>
41 #include <stdlib.h>
42 #include <stdint.h>
43 #include <string.h>
44 
45 /*
46  * We deal with OIDs a lot in their DER form. These have the
47  * advantage of having the length encoded in their second byte,
48  * at least for all the OIDs we're interested in.
49  *
50  * The goal here is to avoid carrying around extra length
51  * information about DER encoded OIDs.
52  */
53 
54 bool
p11_oid_simple(const unsigned char * oid,int len)55 p11_oid_simple (const unsigned char *oid,
56                 int len)
57 {
58 	return (oid != NULL &&
59 	        len > 3 &&                   /* minimum length */
60 	        oid[0] == 0x06 &&            /* simple encoding */
61 	        (oid[1] & 128) == 0 &&       /* short form length */
62 	        (size_t)oid[1] == len - 2);  /* matches length */
63 }
64 
65 unsigned int
p11_oid_hash(const void * oid)66 p11_oid_hash (const void *oid)
67 {
68 	uint32_t hash;
69 	int len;
70 
71 	len = p11_oid_length (oid);
72 	p11_hash_murmur3 (&hash, oid, len, NULL);
73 	return hash;
74 }
75 
76 bool
p11_oid_equal(const void * oid_one,const void * oid_two)77 p11_oid_equal (const void *oid_one,
78                const void *oid_two)
79 {
80 	int len_one;
81 	int len_two;
82 
83 	len_one = p11_oid_length (oid_one);
84 	len_two = p11_oid_length (oid_two);
85 
86 	return (len_one == len_two &&
87 	        memcmp (oid_one, oid_two, len_one) == 0);
88 }
89 
90 int
p11_oid_length(const unsigned char * oid)91 p11_oid_length (const unsigned char *oid)
92 {
93 	assert (oid[0] == 0x06);
94 	assert ((oid[1] & 128) == 0);
95 	return (int)oid[1] + 2;
96 }
97