| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 |
| 15 | #include <limits.h> |
| 16 | #include <stdio.h> |
| 17 | #include <string.h> |
| 18 |
| 19 | #include <openssl/digest.h> |
| 20 | #include <openssl/err.h> |
| 21 | #include <openssl/mem.h> |
| 22 | #include <openssl/obj.h> |
| 23 | #include <openssl/x509.h> |
| 24 |
| 25 | #include "ext_dat.h" |
| 26 | #include "internal.h" |
| 27 |
| 28 |
| 29 | char *i2s_ASN1_OCTET_STRING(const X509V3_EXT_METHOD *method, |
| 30 | const ASN1_OCTET_STRING *oct) { |
| 31 | return x509v3_bytes_to_hex(oct->data, oct->length); |
| 32 | } |
| 33 |
| 34 | ASN1_OCTET_STRING *s2i_ASN1_OCTET_STRING(const X509V3_EXT_METHOD *method, |
| 35 | const X509V3_CTX *ctx, |
| 36 | const char *str) { |
| 37 | size_t len; |
| 38 | uint8_t *data = x509v3_hex_to_bytes(str, &len); |
| 39 | ASN1_OCTET_STRING *oct; |
| 40 | if (data == NULL) { |
| 41 | return NULL; |
| 42 | } |
| 43 | if (len > INT_MAX) { |
| 44 | OPENSSL_PUT_ERROR(X509V3, ERR_R_OVERFLOW); |
| 45 | goto err; |
| 46 | } |
| 47 |
| 48 | oct = ASN1_OCTET_STRING_new(); |
| 49 | if (oct == NULL) { |
| 50 | goto err; |
| 51 | } |
| 52 | ASN1_STRING_set0(oct, data, (int)len); |
| 53 | return oct; |
| 54 |
| 55 | err: |
| 56 | OPENSSL_free(data); |
| 57 | return NULL; |
| 58 | } |
| 59 |
| 60 | static char *i2s_ASN1_OCTET_STRING_cb(const X509V3_EXT_METHOD *method, |
| 61 | void *ext) { |
| 62 | return i2s_ASN1_OCTET_STRING(method, |
| 63 | reinterpret_cast<ASN1_OCTET_STRING *>(ext)); |
| 64 | } |
| 65 |
| 66 | static void *s2i_skey_id(const X509V3_EXT_METHOD *method, const X509V3_CTX *ctx, |
| 67 | const char *str) { |
| 68 | ASN1_OCTET_STRING *oct; |
| 69 | ASN1_BIT_STRING *pk; |
| 70 | unsigned char pkey_dig[EVP_MAX_MD_SIZE]; |
| 71 | unsigned int diglen; |
| 72 |
| 73 | if (strcmp(str, "hash")) { |
| 74 | return s2i_ASN1_OCTET_STRING(method, ctx, str); |
| 75 | } |
| 76 |
| 77 | if (!(oct = ASN1_OCTET_STRING_new())) { |
| 78 | return NULL; |
| 79 | } |
| 80 |
| 81 | if (ctx && (ctx->flags == X509V3_CTX_TEST)) { |
| 82 | return oct; |
| 83 | } |
| 84 |
| 85 | if (!ctx || (!ctx->subject_req && !ctx->subject_cert)) { |
| 86 | OPENSSL_PUT_ERROR(X509V3, X509V3_R_NO_PUBLIC_KEY); |
| 87 | goto err; |
| 88 | } |
| 89 |
| 90 | if (ctx->subject_req) { |
| 91 | pk = ctx->subject_req->req_info->pubkey->public_key; |
| 92 | } else { |
| 93 | pk = ctx->subject_cert->cert_info->key->public_key; |
| 94 | } |
| 95 |
| 96 | if (!pk) { |
| 97 | OPENSSL_PUT_ERROR(X509V3, X509V3_R_NO_PUBLIC_KEY); |
| 98 | goto err; |
| 99 | } |
| 100 |
| 101 | if (!EVP_Digest(pk->data, pk->length, pkey_dig, &diglen, EVP_sha1(), NULL)) { |
| 102 | goto err; |
| 103 | } |
| 104 |
| 105 | if (!ASN1_OCTET_STRING_set(oct, pkey_dig, diglen)) { |
| 106 | goto err; |
| 107 | } |
| 108 |
| 109 | return oct; |
| 110 |
| 111 | err: |
| 112 | ASN1_OCTET_STRING_free(oct); |
| 113 | return NULL; |
| 114 | } |
| 115 |
| 116 | const X509V3_EXT_METHOD v3_skey_id = { |
| 117 | NID_subject_key_identifier, |
| 118 | 0, |
| 119 | ASN1_ITEM_ref(ASN1_OCTET_STRING), |
| 120 | 0, |
| 121 | 0, |
| 122 | 0, |
| 123 | 0, |
| 124 | i2s_ASN1_OCTET_STRING_cb, |
| 125 | s2i_skey_id, |
| 126 | 0, |
| 127 | 0, |
| 128 | 0, |
| 129 | 0, |
| 130 | NULL, |
| 131 | }; |
| 132 |