David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project |
| 3 | * 1999. |
| 4 | */ |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 5 | /* ==================================================================== |
| 6 | * Copyright (c) 1999 The OpenSSL Project. All rights reserved. |
| 7 | * |
| 8 | * Redistribution and use in source and binary forms, with or without |
| 9 | * modification, are permitted provided that the following conditions |
| 10 | * are met: |
| 11 | * |
| 12 | * 1. Redistributions of source code must retain the above copyright |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 13 | * notice, this list of conditions and the following disclaimer. |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 14 | * |
| 15 | * 2. Redistributions in binary form must reproduce the above copyright |
| 16 | * notice, this list of conditions and the following disclaimer in |
| 17 | * the documentation and/or other materials provided with the |
| 18 | * distribution. |
| 19 | * |
| 20 | * 3. All advertising materials mentioning features or use of this |
| 21 | * software must display the following acknowledgment: |
| 22 | * "This product includes software developed by the OpenSSL Project |
| 23 | * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" |
| 24 | * |
| 25 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to |
| 26 | * endorse or promote products derived from this software without |
| 27 | * prior written permission. For written permission, please contact |
| 28 | * licensing@OpenSSL.org. |
| 29 | * |
| 30 | * 5. Products derived from this software may not be called "OpenSSL" |
| 31 | * nor may "OpenSSL" appear in their names without prior written |
| 32 | * permission of the OpenSSL Project. |
| 33 | * |
| 34 | * 6. Redistributions of any form whatsoever must retain the following |
| 35 | * acknowledgment: |
| 36 | * "This product includes software developed by the OpenSSL Project |
| 37 | * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" |
| 38 | * |
| 39 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY |
| 40 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 41 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 42 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR |
| 43 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 44 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 45 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 46 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 47 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 48 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 49 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
| 50 | * OF THE POSSIBILITY OF SUCH DAMAGE. |
| 51 | * ==================================================================== |
| 52 | * |
| 53 | * This product includes cryptographic software written by Eric Young |
| 54 | * (eay@cryptsoft.com). This product includes software written by Tim |
| 55 | * Hudson (tjh@cryptsoft.com). */ |
| 56 | |
| 57 | #include <stdio.h> |
| 58 | |
| 59 | #include <openssl/asn1t.h> |
| 60 | #include <openssl/evp.h> |
| 61 | #include <openssl/obj.h> |
| 62 | #include <openssl/x509.h> |
| 63 | |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 64 | /* |
| 65 | * X509_CERT_AUX routines. These are used to encode additional user |
| 66 | * modifiable data about a certificate. This data is appended to the X509 |
| 67 | * encoding when the *_X509_AUX routines are used. This means that the |
| 68 | * "traditional" X509 routines will simply ignore the extra data. |
| 69 | */ |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 70 | |
| 71 | static X509_CERT_AUX *aux_get(X509 *x); |
| 72 | |
| 73 | ASN1_SEQUENCE(X509_CERT_AUX) = { |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 74 | ASN1_SEQUENCE_OF_OPT(X509_CERT_AUX, trust, ASN1_OBJECT), |
| 75 | ASN1_IMP_SEQUENCE_OF_OPT(X509_CERT_AUX, reject, ASN1_OBJECT, 0), |
| 76 | ASN1_OPT(X509_CERT_AUX, alias, ASN1_UTF8STRING), |
| 77 | ASN1_OPT(X509_CERT_AUX, keyid, ASN1_OCTET_STRING), |
| 78 | ASN1_IMP_SEQUENCE_OF_OPT(X509_CERT_AUX, other, X509_ALGOR, 1) |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 79 | } ASN1_SEQUENCE_END(X509_CERT_AUX) |
| 80 | |
| 81 | IMPLEMENT_ASN1_FUNCTIONS(X509_CERT_AUX) |
| 82 | |
| 83 | static X509_CERT_AUX *aux_get(X509 *x) |
| 84 | { |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 85 | if (!x) |
| 86 | return NULL; |
| 87 | if (!x->aux && !(x->aux = X509_CERT_AUX_new())) |
| 88 | return NULL; |
| 89 | return x->aux; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | int X509_alias_set1(X509 *x, unsigned char *name, int len) |
| 93 | { |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 94 | X509_CERT_AUX *aux; |
| 95 | if (!name) { |
| 96 | if (!x || !x->aux || !x->aux->alias) |
| 97 | return 1; |
| 98 | ASN1_UTF8STRING_free(x->aux->alias); |
| 99 | x->aux->alias = NULL; |
| 100 | return 1; |
| 101 | } |
| 102 | if (!(aux = aux_get(x))) |
| 103 | return 0; |
| 104 | if (!aux->alias && !(aux->alias = ASN1_UTF8STRING_new())) |
| 105 | return 0; |
| 106 | return ASN1_STRING_set(aux->alias, name, len); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | int X509_keyid_set1(X509 *x, unsigned char *id, int len) |
| 110 | { |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 111 | X509_CERT_AUX *aux; |
| 112 | if (!id) { |
| 113 | if (!x || !x->aux || !x->aux->keyid) |
| 114 | return 1; |
| 115 | ASN1_OCTET_STRING_free(x->aux->keyid); |
| 116 | x->aux->keyid = NULL; |
| 117 | return 1; |
| 118 | } |
| 119 | if (!(aux = aux_get(x))) |
| 120 | return 0; |
| 121 | if (!aux->keyid && !(aux->keyid = ASN1_OCTET_STRING_new())) |
| 122 | return 0; |
| 123 | return ASN1_STRING_set(aux->keyid, id, len); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | unsigned char *X509_alias_get0(X509 *x, int *len) |
| 127 | { |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 128 | if (!x->aux || !x->aux->alias) |
| 129 | return NULL; |
| 130 | if (len) |
| 131 | *len = x->aux->alias->length; |
| 132 | return x->aux->alias->data; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | unsigned char *X509_keyid_get0(X509 *x, int *len) |
| 136 | { |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 137 | if (!x->aux || !x->aux->keyid) |
| 138 | return NULL; |
| 139 | if (len) |
| 140 | *len = x->aux->keyid->length; |
| 141 | return x->aux->keyid->data; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | int X509_add1_trust_object(X509 *x, ASN1_OBJECT *obj) |
| 145 | { |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 146 | ASN1_OBJECT *objtmp = OBJ_dup(obj); |
| 147 | if (objtmp == NULL) |
| 148 | goto err; |
| 149 | X509_CERT_AUX *aux = aux_get(x); |
| 150 | if (aux->trust == NULL) { |
| 151 | aux->trust = sk_ASN1_OBJECT_new_null(); |
| 152 | if (aux->trust == NULL) |
| 153 | goto err; |
| 154 | } |
| 155 | if (!sk_ASN1_OBJECT_push(aux->trust, objtmp)) |
| 156 | goto err; |
| 157 | return 1; |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 158 | |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 159 | err: |
| 160 | ASN1_OBJECT_free(objtmp); |
| 161 | return 0; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | int X509_add1_reject_object(X509 *x, ASN1_OBJECT *obj) |
| 165 | { |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 166 | ASN1_OBJECT *objtmp = OBJ_dup(obj); |
| 167 | if (objtmp == NULL) |
| 168 | goto err; |
| 169 | X509_CERT_AUX *aux = aux_get(x); |
| 170 | if (aux->reject == NULL) { |
| 171 | aux->reject = sk_ASN1_OBJECT_new_null(); |
| 172 | if (aux->reject == NULL) |
| 173 | goto err; |
| 174 | } |
| 175 | if (!sk_ASN1_OBJECT_push(aux->reject, objtmp)) |
| 176 | goto err; |
| 177 | return 1; |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 178 | |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 179 | err: |
| 180 | ASN1_OBJECT_free(objtmp); |
| 181 | return 0; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | void X509_trust_clear(X509 *x) |
| 185 | { |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 186 | if (x->aux && x->aux->trust) { |
| 187 | sk_ASN1_OBJECT_pop_free(x->aux->trust, ASN1_OBJECT_free); |
| 188 | x->aux->trust = NULL; |
| 189 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | void X509_reject_clear(X509 *x) |
| 193 | { |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 194 | if (x->aux && x->aux->reject) { |
| 195 | sk_ASN1_OBJECT_pop_free(x->aux->reject, ASN1_OBJECT_free); |
| 196 | x->aux->reject = NULL; |
| 197 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 198 | } |