Coverage Report

Created: 2018-08-29 13:53

/src/openssl/crypto/x509v3/v3_skey.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 1999-2016 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the OpenSSL license (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
8
 */
9
10
#include <stdio.h>
11
#include "internal/cryptlib.h"
12
#include <openssl/x509v3.h>
13
#include "internal/x509_int.h"
14
#include "ext_dat.h"
15
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),
20
    0, 0, 0, 0,
21
    (X509V3_EXT_I2S)i2s_ASN1_OCTET_STRING,
22
    (X509V3_EXT_S2I)s2i_skey_id,
23
    0, 0, 0, 0,
24
    NULL
25
};
26
27
char *i2s_ASN1_OCTET_STRING(X509V3_EXT_METHOD *method,
28
                            const ASN1_OCTET_STRING *oct)
29
0
{
30
0
    return OPENSSL_buf2hexstr(oct->data, oct->length);
31
0
}
32
33
ASN1_OCTET_STRING *s2i_ASN1_OCTET_STRING(X509V3_EXT_METHOD *method,
34
                                         X509V3_CTX *ctx, const char *str)
35
0
{
36
0
    ASN1_OCTET_STRING *oct;
37
0
    long length;
38
0
39
0
    if ((oct = ASN1_OCTET_STRING_new()) == NULL) {
40
0
        X509V3err(X509V3_F_S2I_ASN1_OCTET_STRING, ERR_R_MALLOC_FAILURE);
41
0
        return NULL;
42
0
    }
43
0
44
0
    if ((oct->data = OPENSSL_hexstr2buf(str, &length)) == NULL) {
45
0
        ASN1_OCTET_STRING_free(oct);
46
0
        return NULL;
47
0
    }
48
0
49
0
    oct->length = length;
50
0
51
0
    return oct;
52
0
53
0
}
54
55
static ASN1_OCTET_STRING *s2i_skey_id(X509V3_EXT_METHOD *method,
56
                                      X509V3_CTX *ctx, char *str)
57
0
{
58
0
    ASN1_OCTET_STRING *oct;
59
0
    X509_PUBKEY *pubkey;
60
0
    const unsigned char *pk;
61
0
    int pklen;
62
0
    unsigned char pkey_dig[EVP_MAX_MD_SIZE];
63
0
    unsigned int diglen;
64
0
65
0
    if (strcmp(str, "hash"))
66
0
        return s2i_ASN1_OCTET_STRING(method, ctx, str);
67
0
68
0
    if ((oct = ASN1_OCTET_STRING_new()) == NULL) {
69
0
        X509V3err(X509V3_F_S2I_SKEY_ID, ERR_R_MALLOC_FAILURE);
70
0
        return NULL;
71
0
    }
72
0
73
0
    if (ctx && (ctx->flags == CTX_TEST))
74
0
        return oct;
75
0
76
0
    if (!ctx || (!ctx->subject_req && !ctx->subject_cert)) {
77
0
        X509V3err(X509V3_F_S2I_SKEY_ID, X509V3_R_NO_PUBLIC_KEY);
78
0
        goto err;
79
0
    }
80
0
81
0
    if (ctx->subject_req)
82
0
        pubkey = ctx->subject_req->req_info.pubkey;
83
0
    else
84
0
        pubkey = ctx->subject_cert->cert_info.key;
85
0
86
0
    if (pubkey == NULL) {
87
0
        X509V3err(X509V3_F_S2I_SKEY_ID, X509V3_R_NO_PUBLIC_KEY);
88
0
        goto err;
89
0
    }
90
0
91
0
    X509_PUBKEY_get0_param(NULL, &pk, &pklen, NULL, pubkey);
92
0
93
0
    if (!EVP_Digest(pk, pklen, pkey_dig, &diglen, EVP_sha1(), NULL))
94
0
        goto err;
95
0
96
0
    if (!ASN1_OCTET_STRING_set(oct, pkey_dig, diglen)) {
97
0
        X509V3err(X509V3_F_S2I_SKEY_ID, ERR_R_MALLOC_FAILURE);
98
0
        goto err;
99
0
    }
100
0
101
0
    return oct;
102
0
103
0
 err:
104
0
    ASN1_OCTET_STRING_free(oct);
105
0
    return NULL;
106
0
}