2 * Copyright 1999-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
11 #include "internal/cryptlib.h"
12 #include <openssl/x509v3.h>
13 #include "crypto/x509.h"
16 static ASN1_OCTET_STRING
*s2i_skey_id(X509V3_EXT_METHOD
*method
,
17 X509V3_CTX
*ctx
, char *str
);
18 const X509V3_EXT_METHOD v3_skey_id
= {
19 NID_subject_key_identifier
, 0, ASN1_ITEM_ref(ASN1_OCTET_STRING
),
21 (X509V3_EXT_I2S
)i2s_ASN1_OCTET_STRING
,
22 (X509V3_EXT_S2I
)s2i_skey_id
,
27 char *i2s_ASN1_OCTET_STRING(X509V3_EXT_METHOD
*method
,
28 const ASN1_OCTET_STRING
*oct
)
30 return OPENSSL_buf2hexstr(oct
->data
, oct
->length
);
33 ASN1_OCTET_STRING
*s2i_ASN1_OCTET_STRING(X509V3_EXT_METHOD
*method
,
34 X509V3_CTX
*ctx
, const char *str
)
36 ASN1_OCTET_STRING
*oct
;
39 if ((oct
= ASN1_OCTET_STRING_new()) == NULL
) {
40 X509V3err(X509V3_F_S2I_ASN1_OCTET_STRING
, ERR_R_MALLOC_FAILURE
);
44 if ((oct
->data
= OPENSSL_hexstr2buf(str
, &length
)) == NULL
) {
45 ASN1_OCTET_STRING_free(oct
);
55 static ASN1_OCTET_STRING
*s2i_skey_id(X509V3_EXT_METHOD
*method
,
56 X509V3_CTX
*ctx
, char *str
)
58 ASN1_OCTET_STRING
*oct
;
60 const unsigned char *pk
;
62 unsigned char pkey_dig
[EVP_MAX_MD_SIZE
];
65 if (strcmp(str
, "hash"))
66 return s2i_ASN1_OCTET_STRING(method
, ctx
, str
);
68 if ((oct
= ASN1_OCTET_STRING_new()) == NULL
) {
69 X509V3err(X509V3_F_S2I_SKEY_ID
, ERR_R_MALLOC_FAILURE
);
73 if (ctx
&& (ctx
->flags
== CTX_TEST
))
76 if (!ctx
|| (!ctx
->subject_req
&& !ctx
->subject_cert
)) {
77 X509V3err(X509V3_F_S2I_SKEY_ID
, X509V3_R_NO_PUBLIC_KEY
);
82 pubkey
= ctx
->subject_req
->req_info
.pubkey
;
84 pubkey
= ctx
->subject_cert
->cert_info
.key
;
87 X509V3err(X509V3_F_S2I_SKEY_ID
, X509V3_R_NO_PUBLIC_KEY
);
91 X509_PUBKEY_get0_param(NULL
, &pk
, &pklen
, NULL
, pubkey
);
93 if (!EVP_Digest(pk
, pklen
, pkey_dig
, &diglen
, EVP_sha1(), NULL
))
96 if (!ASN1_OCTET_STRING_set(oct
, pkey_dig
, diglen
)) {
97 X509V3err(X509V3_F_S2I_SKEY_ID
, ERR_R_MALLOC_FAILURE
);
104 ASN1_OCTET_STRING_free(oct
);