Absolute File Name: | /home/opencoverage/opencoverage/guest-scripts/openssl/src/crypto/evp/evp_pkey.c |
Source code | Switch to Preprocessed file |
Line | Source | Count | ||||||
---|---|---|---|---|---|---|---|---|
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 <stdlib.h> | - | ||||||
12 | #include "internal/cryptlib.h" | - | ||||||
13 | #include <openssl/x509.h> | - | ||||||
14 | #include <openssl/rand.h> | - | ||||||
15 | #include "internal/asn1_int.h" | - | ||||||
16 | #include "internal/evp_int.h" | - | ||||||
17 | #include "internal/x509_int.h" | - | ||||||
18 | - | |||||||
19 | /* Extract a private key from a PKCS8 structure */ | - | ||||||
20 | - | |||||||
21 | EVP_PKEY *EVP_PKCS82PKEY(const PKCS8_PRIV_KEY_INFO *p8) | - | ||||||
22 | { | - | ||||||
23 | EVP_PKEY *pkey = NULL; | - | ||||||
24 | const ASN1_OBJECT *algoid; | - | ||||||
25 | char obj_tmp[80]; | - | ||||||
26 | - | |||||||
27 | if (!PKCS8_pkey_get0(&algoid, NULL, NULL, NULL, p8))
| 0-2841 | ||||||
28 | return NULL; never executed: return ((void *)0) ; | 0 | ||||||
29 | - | |||||||
30 | if ((pkey = EVP_PKEY_new()) == NULL) {
| 0-2841 | ||||||
31 | EVPerr(EVP_F_EVP_PKCS82PKEY, ERR_R_MALLOC_FAILURE); | - | ||||||
32 | return NULL; never executed: return ((void *)0) ; | 0 | ||||||
33 | } | - | ||||||
34 | - | |||||||
35 | if (!EVP_PKEY_set_type(pkey, OBJ_obj2nid(algoid))) {
| 1-2840 | ||||||
36 | EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_UNSUPPORTED_PRIVATE_KEY_ALGORITHM); | - | ||||||
37 | i2t_ASN1_OBJECT(obj_tmp, 80, algoid); | - | ||||||
38 | ERR_add_error_data(2, "TYPE=", obj_tmp); | - | ||||||
39 | goto error; executed 1 time by 1 test: goto error; Executed by:
| 1 | ||||||
40 | } | - | ||||||
41 | - | |||||||
42 | if (pkey->ameth->priv_decode) {
| 0-2840 | ||||||
43 | if (!pkey->ameth->priv_decode(pkey, p8)) {
| 12-2828 | ||||||
44 | EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_PRIVATE_KEY_DECODE_ERROR); | - | ||||||
45 | goto error; executed 12 times by 1 test: goto error; Executed by:
| 12 | ||||||
46 | } | - | ||||||
47 | } else { executed 2828 times by 1 test: end of block Executed by:
| 2828 | ||||||
48 | EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_METHOD_NOT_SUPPORTED); | - | ||||||
49 | goto error; never executed: goto error; | 0 | ||||||
50 | } | - | ||||||
51 | - | |||||||
52 | return pkey; executed 2828 times by 1 test: return pkey; Executed by:
| 2828 | ||||||
53 | - | |||||||
54 | error: | - | ||||||
55 | EVP_PKEY_free(pkey); | - | ||||||
56 | return NULL; executed 13 times by 1 test: return ((void *)0) ; Executed by:
| 13 | ||||||
57 | } | - | ||||||
58 | - | |||||||
59 | /* Turn a private key into a PKCS8 structure */ | - | ||||||
60 | - | |||||||
61 | PKCS8_PRIV_KEY_INFO *EVP_PKEY2PKCS8(EVP_PKEY *pkey) | - | ||||||
62 | { | - | ||||||
63 | PKCS8_PRIV_KEY_INFO *p8 = PKCS8_PRIV_KEY_INFO_new(); | - | ||||||
64 | if (p8 == NULL) {
| 0-29 | ||||||
65 | EVPerr(EVP_F_EVP_PKEY2PKCS8, ERR_R_MALLOC_FAILURE); | - | ||||||
66 | return NULL; never executed: return ((void *)0) ; | 0 | ||||||
67 | } | - | ||||||
68 | - | |||||||
69 | if (pkey->ameth) {
| 0-29 | ||||||
70 | if (pkey->ameth->priv_encode) {
| 0-29 | ||||||
71 | if (!pkey->ameth->priv_encode(p8, pkey)) {
| 0-29 | ||||||
72 | EVPerr(EVP_F_EVP_PKEY2PKCS8, EVP_R_PRIVATE_KEY_ENCODE_ERROR); | - | ||||||
73 | goto error; never executed: goto error; | 0 | ||||||
74 | } | - | ||||||
75 | } else { executed 29 times by 1 test: end of block Executed by:
| 29 | ||||||
76 | EVPerr(EVP_F_EVP_PKEY2PKCS8, EVP_R_METHOD_NOT_SUPPORTED); | - | ||||||
77 | goto error; never executed: goto error; | 0 | ||||||
78 | } | - | ||||||
79 | } else { | - | ||||||
80 | EVPerr(EVP_F_EVP_PKEY2PKCS8, EVP_R_UNSUPPORTED_PRIVATE_KEY_ALGORITHM); | - | ||||||
81 | goto error; never executed: goto error; | 0 | ||||||
82 | } | - | ||||||
83 | return p8; executed 29 times by 1 test: return p8; Executed by:
| 29 | ||||||
84 | error: | - | ||||||
85 | PKCS8_PRIV_KEY_INFO_free(p8); | - | ||||||
86 | return NULL; never executed: return ((void *)0) ; | 0 | ||||||
87 | } | - | ||||||
88 | - | |||||||
89 | /* EVP_PKEY attribute functions */ | - | ||||||
90 | - | |||||||
91 | int EVP_PKEY_get_attr_count(const EVP_PKEY *key) | - | ||||||
92 | { | - | ||||||
93 | return X509at_get_attr_count(key->attributes); never executed: return X509at_get_attr_count(key->attributes); | 0 | ||||||
94 | } | - | ||||||
95 | - | |||||||
96 | int EVP_PKEY_get_attr_by_NID(const EVP_PKEY *key, int nid, int lastpos) | - | ||||||
97 | { | - | ||||||
98 | return X509at_get_attr_by_NID(key->attributes, nid, lastpos); never executed: return X509at_get_attr_by_NID(key->attributes, nid, lastpos); | 0 | ||||||
99 | } | - | ||||||
100 | - | |||||||
101 | int EVP_PKEY_get_attr_by_OBJ(const EVP_PKEY *key, const ASN1_OBJECT *obj, | - | ||||||
102 | int lastpos) | - | ||||||
103 | { | - | ||||||
104 | return X509at_get_attr_by_OBJ(key->attributes, obj, lastpos); never executed: return X509at_get_attr_by_OBJ(key->attributes, obj, lastpos); | 0 | ||||||
105 | } | - | ||||||
106 | - | |||||||
107 | X509_ATTRIBUTE *EVP_PKEY_get_attr(const EVP_PKEY *key, int loc) | - | ||||||
108 | { | - | ||||||
109 | return X509at_get_attr(key->attributes, loc); never executed: return X509at_get_attr(key->attributes, loc); | 0 | ||||||
110 | } | - | ||||||
111 | - | |||||||
112 | X509_ATTRIBUTE *EVP_PKEY_delete_attr(EVP_PKEY *key, int loc) | - | ||||||
113 | { | - | ||||||
114 | return X509at_delete_attr(key->attributes, loc); never executed: return X509at_delete_attr(key->attributes, loc); | 0 | ||||||
115 | } | - | ||||||
116 | - | |||||||
117 | int EVP_PKEY_add1_attr(EVP_PKEY *key, X509_ATTRIBUTE *attr) | - | ||||||
118 | { | - | ||||||
119 | if (X509at_add1_attr(&key->attributes, attr))
| 0 | ||||||
120 | return 1; never executed: return 1; | 0 | ||||||
121 | return 0; never executed: return 0; | 0 | ||||||
122 | } | - | ||||||
123 | - | |||||||
124 | int EVP_PKEY_add1_attr_by_OBJ(EVP_PKEY *key, | - | ||||||
125 | const ASN1_OBJECT *obj, int type, | - | ||||||
126 | const unsigned char *bytes, int len) | - | ||||||
127 | { | - | ||||||
128 | if (X509at_add1_attr_by_OBJ(&key->attributes, obj, type, bytes, len))
| 0 | ||||||
129 | return 1; never executed: return 1; | 0 | ||||||
130 | return 0; never executed: return 0; | 0 | ||||||
131 | } | - | ||||||
132 | - | |||||||
133 | int EVP_PKEY_add1_attr_by_NID(EVP_PKEY *key, | - | ||||||
134 | int nid, int type, | - | ||||||
135 | const unsigned char *bytes, int len) | - | ||||||
136 | { | - | ||||||
137 | if (X509at_add1_attr_by_NID(&key->attributes, nid, type, bytes, len))
| 0 | ||||||
138 | return 1; never executed: return 1; | 0 | ||||||
139 | return 0; never executed: return 0; | 0 | ||||||
140 | } | - | ||||||
141 | - | |||||||
142 | int EVP_PKEY_add1_attr_by_txt(EVP_PKEY *key, | - | ||||||
143 | const char *attrname, int type, | - | ||||||
144 | const unsigned char *bytes, int len) | - | ||||||
145 | { | - | ||||||
146 | if (X509at_add1_attr_by_txt(&key->attributes, attrname, type, bytes, len))
| 0 | ||||||
147 | return 1; never executed: return 1; | 0 | ||||||
148 | return 0; never executed: return 0; | 0 | ||||||
149 | } | - | ||||||
Source code | Switch to Preprocessed file |