/src/p11-kit/common/oid.c
Line | Count | Source |
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 |
55 | | p11_oid_simple (const unsigned char *oid, |
56 | | int len) |
57 | 0 | { |
58 | 0 | return (oid != NULL && |
59 | 0 | len > 3 && /* minimum length */ |
60 | 0 | oid[0] == 0x06 && /* simple encoding */ |
61 | 0 | (oid[1] & 128) == 0 && /* short form length */ |
62 | 0 | (size_t)oid[1] == len - 2); /* matches length */ |
63 | 0 | } |
64 | | |
65 | | unsigned int |
66 | | p11_oid_hash (const void *oid) |
67 | 0 | { |
68 | 0 | uint32_t hash; |
69 | 0 | int len; |
70 | |
|
71 | 0 | len = p11_oid_length (oid); |
72 | 0 | p11_hash_murmur3 (&hash, oid, len, NULL); |
73 | 0 | return hash; |
74 | 0 | } |
75 | | |
76 | | bool |
77 | | p11_oid_equal (const void *oid_one, |
78 | | const void *oid_two) |
79 | 0 | { |
80 | 0 | int len_one; |
81 | 0 | int len_two; |
82 | |
|
83 | 0 | len_one = p11_oid_length (oid_one); |
84 | 0 | len_two = p11_oid_length (oid_two); |
85 | |
|
86 | 0 | return (len_one == len_two && |
87 | 0 | memcmp (oid_one, oid_two, len_one) == 0); |
88 | 0 | } |
89 | | |
90 | | int |
91 | | p11_oid_length (const unsigned char *oid) |
92 | 3.49k | { |
93 | 3.49k | assert (oid[0] == 0x06); |
94 | 3.49k | assert ((oid[1] & 128) == 0); |
95 | 3.49k | return (int)oid[1] + 2; |
96 | 3.49k | } |